package com.example.demo.controller;

import com.example.demo.VO.MyCourseVO;
import com.example.demo.VO.Result;
import com.example.demo.entity.Course;
import com.example.demo.entity.MyCourse;
import com.example.demo.service.CourseService;
import com.example.demo.service.MyCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (MyCourse)表控制层
 *
 * @author hjl
 * @since 2020-11-30 19:33:02
 */
@RestController
@RequestMapping("myCourse")
public class MyCourseController {
    /**
     * 服务对象
     */
    @Resource
    private MyCourseService myCourseService;

    @Autowired
    private CourseService  courseService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public MyCourse selectOne(Integer id) {
        return this.myCourseService.queryById(id);
    }

    @PostMapping("addMyCourse")
    public Result addMyCourse(MyCourse myCourse){
        myCourseService.insert(myCourse);
        return Result.success(myCourseService.queryByExample(myCourse));
    }
    @DeleteMapping("delMyCourse")
    public Result delMyCourse(int CourseId,int Userid)
    {
        MyCourse myCourse=new MyCourse();
        myCourse.setCourseId(CourseId);
        myCourse.setUserId(Userid);
        return Result.success(myCourseService.deleteById(myCourseService.queryByExample(myCourse).get(0).getId()));
    }

    @PostMapping("selectUserAllCourse")
    public Result selectUserAllCourse(int userid)
    {
        System.out.println("aaa");
        Course course=new Course();
        MyCourse myCourse=new MyCourse();
        course.setUserId(userid);
        myCourse.setUserId(userid);
        List<MyCourse> myCourseList=myCourseService.queryByExample(myCourse);
        List<Course> list=new ArrayList<>();
        for(int i=0;i<myCourseList.size();i++)
        {
            list.add(courseService.queryById(myCourseList.get(i).getCourseId()));
        }
        System.out.println(list.toString());

          return Result.success(new MyCourseVO(list,courseService.queryByExample(course)));
    }



}